# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import json
from scrapy.pipelines.images import ImagesPipeline
from scrapy.pipelines.files import FilesPipeline
from scrapy import Request
import os


class QidianImgsPipeline:

    def __init__(self):
        self.file = open('qidoan.json', 'w', encoding='utf-8')

    def close_spider(self, spider):
        self.file.close()

    def process_item(self, item, spider):
        line = json.dumps(dict(item), ensure_ascii=False) + '\n'
        self.file.write(line)
        return item


class DownloadimagesPipeline(ImagesPipeline):

    def file_path(self, request, response=None, info=None):
        # 获取request中的item
        item = request.meta['item']
        # 文件格式
        images_type = 'jpg'
        # 修改使用item中的书名为图片名
        image_name = u'full/{0}.{1}'.format(item['title'], images_type)
        return image_name

    def get_media_requests(self, item, info):
        #
        file = item['images'][0]['path']
        os.remove('.\\images\\{}'.format(file))
        for image_url in item['image_urls']:
            # 为request带上meta参数，把item传递过去
            yield Request(image_url, meta={'item': item})

